package com.atguigu.utils;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;

public class IntSource implements SourceFunction<Integer> {
    //一直跑 生成数据
    private boolean running = true;
    //随机数
    private Random random = new Random();

    //run用来发出数据源
    @Override
    public void run(SourceContext<Integer> ctx) throws Exception {
        while (running){
            //生成0~1000的随机整数
            ctx.collect(random.nextInt(1000));
            //线程睡1000毫秒，再生成下一个
            Thread.sleep(1000);
        }

    }

    @Override
    public void cancel() {
        //停止数据生成 cancel用来取消数据源
        running = false;
    }
}
